package cn.edu.imau.zy.exam.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;
import java.util.Arrays;


import cn.edu.imau.zy.common.core.entity.examination.EQuestionInfo;
import cn.edu.imau.zy.common.utils.poi.ExcelUtil;
import cn.edu.imau.zy.exam.service.IEQuestionInfoService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import cn.edu.imau.zy.common.annotation.LoginUser;
import cn.edu.imau.zy.common.core.entity.system.SysUser;


import cn.edu.imau.zy.common.core.domain.R;
import cn.edu.imau.zy.common.core.controller.BaseController;
import org.springframework.web.multipart.MultipartFile;

/**
 * 试题信息 提供者
 * 
 * @author gj
 * @date 2021-12-04
 */
@RestController
@RequestMapping("exam/questionInfo")
public class EQuestionInfoController extends BaseController
{
	
	@Autowired
	private IEQuestionInfoService eQuestionInfoService;
	
	/**
	 * 查询${tableComment}
	 */
	@GetMapping("get/{id}")
	public EQuestionInfo get(@PathVariable("id") Long id)
	{
		return eQuestionInfoService.getById(id);
		
	}
	
	/**
	 * 查询试题信息列表
	 */
	@GetMapping("list")
	public R list(EQuestionInfo eQuestionInfo)
	{
		startPage();
        return result(eQuestionInfoService.queryList(eQuestionInfo));
	}
	
	
	/**
	 * 新增保存试题信息
	 */
	@PostMapping("save")
	public R addSave(@RequestBody EQuestionInfo eQuestionInfo,@LoginUser SysUser user)
	{
		return toAjax(eQuestionInfoService.save(eQuestionInfo));
	}

	/**
	 * 修改保存试题信息
	 */
	@PostMapping("update")
	public R editSave(@RequestBody EQuestionInfo eQuestionInfo,@LoginUser SysUser user)
	{
		return toAjax(eQuestionInfoService.updateById(eQuestionInfo));
	}
	
	/**
	 * 删除${tableComment}
	 */
	@PostMapping("remove")
	public R remove(String ids)
	{		
		return toAjax(eQuestionInfoService.removeById(ids));
	}

	@PostMapping("importQuestion")
	public R importQuestion(@RequestParam("file") MultipartFile file) throws Exception {

		// 获取文件名称
		String originalFilename = file.getOriginalFilename();
		// 判断是否是以指定文件扩展名
		if(originalFilename.endsWith(".xlsx") || originalFilename.endsWith(".xls")){
			boolean b =	eQuestionInfoService.importQuestion(file);
		}else{
			return R.error("只能上传表格，请重新选择文件.");
		}

		return  R.ok();
	}

	
}
